Laravel Octane supercharges your application's performance by serving your application using high-powered application servers, including Open Swoole, Swoole, and RoadRunner. Octane boots your application once, keeps it in memory, and then feeds it requests at supersonic speeds.
讓 PHP 能以多執行緒的方式運行 。安裝好後一樣能使用 php artisan serve 作為開發時的測試環境,部署時則不用依賴 php-fpm ,透過指令能夠運行在指定的 port 上,之後在 nginx 做 proxy_pass 就能上線了。
pecl install swoole
# 安裝選項全選擇[no]
透過 php --ini 找到 php.ini 位置,並在 php.ini 最後加上
extension=swoole.so
composer require laravel/octane
php artisan octane:install
# 安裝過程選項 => 選擇 swoole
php artisan octane:start --watch
或是不掛載 octane 直接使用原本的
php artisan serve
⚠️ 如果部署到HTTPS環境,記得修改 .env 的 OCTANE_HTTPS 設定,讓靜態檔案也能以 HTTPS 掛載
OCTANE_HTTPS=true
php artisan octane:start --port=<port>
我會用 PM2 去執行啟動指令
# pm2.json example
{
  "name": "laravel-octane-server",
  "script": "current/artisan octane:start --port=8090",
  "max_memory_restart": "4G",
  "autorestart": true,
  "log": "./pm2-octane.log"
}
執行
pm2 start ./pm2.json
# 跑起來後記得下 save,防止機器重開機後消失掛載
pm2 save
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
 
server {
    listen 80;
    listen [::]:80;
    server_name domain.com;
    server_tokens off;
    root /home/forge/domain.com/public;
 
    index index.php;
 
    charset utf-8;
 
    location /index.php {
        try_files /not_exists @octane;
    }
 
    location / {
        try_files $uri $uri/ @octane;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    access_log off;
    error_log  /var/log/nginx/domain.com-error.log error;
 
    error_page 404 /index.php;
 
    location @octane {
        set $suffix "";
 
        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }
 
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
 
        proxy_pass http://127.0.0.1:8000$suffix;
    }
}